<template>
  <div>
    <zbTable ref="tableRef" :api-fun="get_list" @action="action" />
    <!-- 新增/编辑弹窗 -->
    <user-edit
      v-if="showEdit"
      v-model:visible="showEdit"
      :actionStatus="actionStatus"
      :editData="editData"
      @done="done"
    />
    <!-- 盘点报告弹窗 -->
    <report
      v-if="showReport"
      v-model:visible="showReport"
      :reportCode="reportCode"
    />
  </div>
</template>

<script setup>
  import { ref, onMounted } from 'vue';
  import { message, Modal } from 'ant-design-vue';
  import { useRouter } from 'vue-router';
  import { actionFn } from '@/utils/action.js';
  import {
    PlusOutlined,
    UploadOutlined,
    ClearOutlined
  } from '@ant-design/icons-vue';
  import { daochuFn } from '@/utils/daochu.js';
  import userEdit from './components/user-edit.vue';
  import report from './components/report.vue';
  // import zbTable from '@/components/zbTable/jewelryPdIndex.vue';
  import zbTable from '@/components/zbTable/receiptsTenantCheckIndex.vue';
  import {
    get_list,
    continue_check,
    check_cancel
  } from '@/api/jewelry/receipts_store_check';
  const { push } = useRouter();
  // 是否显示编辑弹窗
  const showEdit = ref(false);
  // 编辑数据
  const editData = ref({});
  // 编辑按钮数据
  const actionData = ref({});
  // 获取子组件
  const tableRef = ref(null);
  const actionStatus = ref('add');
  const showReport = ref(false);
  const reportCode = ref(null);
  /* 打开添加弹窗 */
  const openEdit = (row) => {
    editData.value.id = '';
    editData.value.code = '';
    showEdit.value = true;
    actionStatus.value = 'add';
    actionData.value = { url: '/warehouse/receipts_store_check/add' };
  };
  const optBtn = ref([]);
  onMounted(() => {});
  const action = (res) => {
    console.log('res==', res);
    editData.value.id = res.row_data.id;
    editData.value.code = res.row_data.code;
    actionData.value = res.action_data;
    if (res.action_data.type == 'add') {
      showEdit.value = true;
      actionStatus.value = 'add';
      actionData.value = { url: res.row_data.url };
    } else if (res.action_data.type == 'continue') {
      /* continue_check({ code: res.row_data.code })
        .then((r) => {
          toDetail(res.row_data.code);
        })
        .catch((e) => {
          message.error(e);
        }); */
      toDetail(res.row_data.code);
    } else if (res.action_data.type == 'report') {
      reportCode.value = res.row_data.code;
      showReport.value = true;
      console.log('reportCode', reportCode.value);
    } else if (res.action_data.type == 'cancel') {
      Modal.confirm({
        title: '提示',
        content: '确定取消盘点?',
        maskClosable: true,
        onOk: () => {
          cancelPd(); //取消盘点
        }
      });
    } else if (res.action_data.type == 'delete') {
      Modal.confirm({
        title: '提示',
        content: '确定删除?',
        maskClosable: true,
        onOk: () => {
          let data = {
            url: res.action_data.url
          };
          actionFn(data, { code: res.row_data.code }, reloadTable);
        }
      });
    }
  };
  const cancelPd = () => {
    check_cancel({ code: editData.value.code })
      .then((res) => {
        reloadTable();
        message.success(res.msg);
      })
      .catch((e) => {
        message.error(e);
      });
  };
  const toDetail = (code) => {
    push({
      path: '/warehouse/receipts_store_check/detail',
      query: { code: code }
    });
  };
  const daochu = () => {
    let data = {
      url: 'warehouse/receipts_store_check/export ',
      filename: '门店盘点列表'
    };
    daochuFn(data);
  };
  const done = (form) => {
    actionFn(actionData.value, form, reloadTable);
  };
  const reloadTable = () => {
    showEdit.value = false;
    tableRef.value.reload();
  };
</script>
